package net.stancristian.restnews;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NewsItemAdapter extends ArrayAdapter<NewsItemSmall> {

	private final Context context;
	private final ArrayList<NewsItemSmall> values;
	private SimpleDateFormat myFormat = new SimpleDateFormat("dd MMM yyyy");
	
	public NewsItemAdapter(Context context, ArrayList<NewsItemSmall> values) {
		super(context, R.layout.news_list_item, values);
		this.context = context;
		this.values = values;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View rowView = inflater.inflate(R.layout.news_list_item, parent, false);
	    
	    NewsItemSmall newsItem = values.get(position);
	    TextView textViewDescription = (TextView) rowView.findViewById(R.id.textViewDescription);
	    TextView textViewTitle = (TextView) rowView.findViewById(R.id.textViewNewsTitle);
	    TextView textViewCreationDate = (TextView) rowView.findViewById(R.id.textViewCreationDate);
	    RelativeLayout layout = (RelativeLayout) rowView.findViewById(R.id.relativeLayoutNewsItem);
	    
	    textViewTitle.setText(newsItem.Title);
	    textViewDescription.setText(newsItem.Summary);
	    
	    textViewCreationDate.setText(myFormat.format(newsItem.ModificationDate));
	    
	    if(!newsItem.IsViewed)
	    {
	    	layout.setBackgroundColor(Color.WHITE);
	    }
	    
	    return rowView;
	}
	
}
